/*
 * frmUsers.java
 *
 * Created on 3. März 2008, 20:00
 */

package com.googlecode.oskis.admin;
import com.googlecode.oskis.library.misc.ColorConstants;
import com.googlecode.oskis.library.misc.TimedDialog;
import com.googlecode.oskis.library.date.DateHelper;
import com.googlecode.oskis.library.date.NOW;
import com.googlecode.oskis.library.db.ComboBoxFiller;
import com.googlecode.oskis.library.db.ListBoxFiller;
import com.googlecode.oskis.library.db.ComboBoxEintrag;
import com.googlecode.oskis.library.db.DBQuery;
import com.googlecode.oskis.library.db.SQLErrorDialog;
import com.googlecode.oskis.library.security.CryptedPasswd;
import com.googlecode.oskis.library.db.SystemParameter;
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.DefaultListModel;


/**
 *
 * @author  thomas
 */
public class frmUsers extends javax.swing.JFrame {
    
    
    private Connection db;
    private DBQuery q;
    
    /** Creates new form frmUsers */
    public frmUsers(Connection db) {
        initComponents();
        this.db=db;
        q=new DBQuery(db);
        ComboBoxFiller cbf=new ComboBoxFiller(db);
        ListBoxFiller lbf = new ListBoxFiller(db);        
        NOW now=new NOW();
        String n="to_timestamp('"+now.getFormattedDate()+"','DD.MM.YYYY HH24:MI:SS')";        
        cbf.fill(cmbPersonalid, "select personalid,name||', '||vorname from xpersonal where "+n+" between datf and datt order by name,vorname");
        lbf.fill(lstEinrichtungen,"select einrichtung,name from xeinrichtung where "+n+" between datf and datt");
        refreshTable();
    }
    
    
    
    
    
   private void refreshTable() {
        NOW now=new NOW();
        String n="to_timestamp('"+now.getFormattedDate()+"','DD.MM.YYYY HH24:MI:SS')";
        String SQL="select u.username,p.name||', '||p.vorname as name " +
                "   from xusers u,xpersonal p" +
                "   where u.personalid=p.personalid" +
                "   and "+n+" between u.datf and u.datt "+
                "   and "+n+" between p.datf and p.datt " +
                "   order by u.username";
        if (q.executeQuery(SQL)) {
            tblUsers.setModel(q.getTableModel());
        } else {
            JOptionPane.showMessageDialog(this,q.getSQLError());
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblBenutzername = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        lblMitarbeiter = new javax.swing.JLabel();
        cmbPersonalid = new javax.swing.JComboBox();
        lblPasswd = new javax.swing.JLabel();
        txtPasswd = new javax.swing.JPasswordField();
        lblPasswdInvalid = new javax.swing.JLabel();
        chkPasswdInvalid = new javax.swing.JCheckBox();
        lblPasswdExpires = new javax.swing.JLabel();
        txtPasswdExpires = new javax.swing.JTextField();
        lblDatf = new javax.swing.JLabel();
        txtDatf = new javax.swing.JTextField();
        lblDatt = new javax.swing.JLabel();
        txtDatt = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblUsers = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstEinrichtungen = new javax.swing.JList();
        btnAutoExpire = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Benutzer");

        lblBenutzername.setText("Benutzername");

        txtUsername.setNextFocusableComponent(cmbPersonalid);
        txtUsername.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtUsernameFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtUsernameFocusLost(evt);
            }
        });

        lblMitarbeiter.setText("Mitarbeiter");

        cmbPersonalid.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbPersonalid.setNextFocusableComponent(txtPasswd);

        lblPasswd.setText("Passwort");

        txtPasswd.setNextFocusableComponent(chkPasswdInvalid);
        txtPasswd.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtPasswdFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtPasswdFocusLost(evt);
            }
        });

        lblPasswdInvalid.setText("Passwort ungültig");

        chkPasswdInvalid.setNextFocusableComponent(txtPasswdExpires);

        lblPasswdExpires.setText("Passwd gült. bis");

        txtPasswdExpires.setNextFocusableComponent(txtDatf);
        txtPasswdExpires.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtPasswdExpiresFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtPasswdExpiresFocusLost(evt);
            }
        });

        lblDatf.setText("Gültig von");

        txtDatf.setNextFocusableComponent(txtDatt);
        txtDatf.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtDatfFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtDatfFocusLost(evt);
            }
        });

        lblDatt.setText("Gültig bis");

        txtDatt.setNextFocusableComponent(txtUsername);
        txtDatt.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtDattFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtDattFocusLost(evt);
            }
        });

        jButton1.setMnemonic('N');
        jButton1.setText("Neu");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setMnemonic('E');
        jButton2.setText("Ändern");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setMnemonic('L');
        jButton3.setText("Löschen");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        tblUsers.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblUsers.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblUsersMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblUsers);

        lstEinrichtungen.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(lstEinrichtungen);

        btnAutoExpire.setText("Auto");
        btnAutoExpire.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAutoExpireActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblPasswdInvalid)
                            .addComponent(lblBenutzername)
                            .addComponent(lblMitarbeiter)
                            .addComponent(lblPasswd)
                            .addComponent(lblPasswdExpires)
                            .addComponent(lblDatf)
                            .addComponent(lblDatt))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmbPersonalid, 0, 492, Short.MAX_VALUE)
                            .addComponent(txtUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
                            .addComponent(txtPasswd, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
                            .addComponent(txtDatf, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
                            .addComponent(txtDatt, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(chkPasswdInvalid)
                                    .addComponent(txtPasswdExpires, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnAutoExpire))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblBenutzername)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblMitarbeiter)
                    .addComponent(cmbPersonalid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPasswd)
                    .addComponent(txtPasswd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPasswdInvalid)
                    .addComponent(chkPasswdInvalid))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPasswdExpires)
                    .addComponent(btnAutoExpire)
                    .addComponent(txtPasswdExpires, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDatf)
                    .addComponent(txtDatf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDatt)
                    .addComponent(txtDatt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane2)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtUsernameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtUsernameFocusGained
        txtUsername.selectAll();
        txtUsername.setBackground(ColorConstants.COLOR_FOCUS);
    }//GEN-LAST:event_txtUsernameFocusGained

    private void txtPasswdFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPasswdFocusGained
        txtPasswd.selectAll();
        txtPasswd.setBackground(ColorConstants.COLOR_FOCUS);
    }//GEN-LAST:event_txtPasswdFocusGained

    private void txtPasswdExpiresFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPasswdExpiresFocusGained
        txtPasswdExpires.selectAll();
        txtPasswdExpires.setBackground(ColorConstants.COLOR_FOCUS);
    }//GEN-LAST:event_txtPasswdExpiresFocusGained

    private void txtDatfFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtDatfFocusGained
        txtDatf.selectAll();
        txtDatf.setBackground(ColorConstants.COLOR_FOCUS);
    }//GEN-LAST:event_txtDatfFocusGained

    private void txtDattFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtDattFocusGained
        txtDatt.selectAll();
        txtDatt.setBackground(ColorConstants.COLOR_FOCUS);
    }//GEN-LAST:event_txtDattFocusGained

    private void txtUsernameFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtUsernameFocusLost
        txtUsername.setBackground(ColorConstants.COLOR_NO_FOCUS);
    }//GEN-LAST:event_txtUsernameFocusLost
      
    private void txtPasswdFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPasswdFocusLost
        txtPasswd.setBackground(ColorConstants.COLOR_NO_FOCUS);
    }//GEN-LAST:event_txtPasswdFocusLost

    private void txtPasswdExpiresFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPasswdExpiresFocusLost
        txtPasswdExpires.setBackground(ColorConstants.COLOR_NO_FOCUS);
        DateHelper dh=new DateHelper(txtPasswdExpires.getText());
        dh.setFormatString("dd.MM.yyyy");
        if (dh.getFormatSuccess()) {
            txtPasswdExpires.setText(dh.getFormattedDate());
        }        
    }//GEN-LAST:event_txtPasswdExpiresFocusLost

    private void txtDatfFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtDatfFocusLost
        txtDatf.setBackground(ColorConstants.COLOR_NO_FOCUS);
        DateHelper dh=new DateHelper(txtDatf.getText());
        dh.setFormatString("dd.MM.yyyy HH:mm:ss");
        if (dh.getFormatSuccess()) {
            txtDatf.setText(dh.getFormattedDate());
        }           
    }//GEN-LAST:event_txtDatfFocusLost

    private void txtDattFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtDattFocusLost
        txtDatt.setBackground(ColorConstants.COLOR_NO_FOCUS);
        DateHelper dh=new DateHelper(txtDatt.getText());
        dh.setFormatString("dd.MM.yyyy HH:mm:ss");
        if (dh.getFormatSuccess()) {
            txtDatt.setText(dh.getFormattedDate());
        }            
    }//GEN-LAST:event_txtDattFocusLost

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String username=txtUsername.getText();
        String personalid=((ComboBoxEintrag)cmbPersonalid.getSelectedItem()).getKey();
        String passwd = new String(txtPasswd.getPassword());
        passwd=new CryptedPasswd(passwd,username.substring(2)).getEncryptedPasswd();
        String passwdinvalid;
        if (chkPasswdInvalid.isSelected()) {
            passwdinvalid="1";
        } else {
            passwdinvalid="0";
        }
        String passwdexpires=txtPasswdExpires.getText();
        String datf=txtDatf.getText();
        String datt=txtDatt.getText();
        String SQL="insert into xusers" +
                "   (username,personalid,passwd,passwdinvalid,passwdexpires,datf,datt) values (" +
                "   '"+username+"',"+personalid+",'"+passwd+"',"+passwdinvalid+",to_date('"+passwdexpires+"','DD.MM.YYYY'),"+
                "   to_timestamp('"+datf+"','DD.MM.YYYY HH24:MI:SS'),to_timestamp('"+datt+"','DD.MM.YYYY HH24:MI:SS'))";
        try {
            Statement st=db.createStatement();
            st.execute(SQL);           
            SQL="delete from lusereinrichtung where username='"+username+"'";
            st.execute(SQL);
            for (int i=0;i<lstEinrichtungen.getSelectedValues().length;i++) {
                ComboBoxEintrag cbe=(ComboBoxEintrag)lstEinrichtungen.getModel().getElementAt(lstEinrichtungen.getSelectedIndices()[i]);
                SQL="insert into lusereinrichtung (username,einrichtung) values('"+username+"','"+cbe.getKey()+"')";
                st.execute(SQL);
            } 
            st.close();
            refreshTable();
        } catch (SQLException e) {
            new SQLErrorDialog(this,"PG",e,SQL).setVisible(true);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void tblUsersMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblUsersMouseClicked
        int row=tblUsers.getSelectedRow();
        String username=(String)tblUsers.getValueAt(row,0);
        String SQL="select personalid,passwdinvalid,to_char(passwdexpires,'DD.MM.YYYY') as passwdexpires," +
                "   to_char(datf,'DD.MM.YYYY HH24:MI:SS') as datf,to_char(datt,'DD.MM.YYYY HH24:MI:SS') as datt" +
                "   from xusers" +
                "   where username='"+username+"'";
        try {
            Statement st=db.createStatement();
            Statement st2=db.createStatement();
            ResultSet rs=st.executeQuery(SQL);
            if (rs.next()) {
                txtUsername.setText(username);
                cmbPersonalid.setSelectedItem(new ComboBoxEintrag(rs.getString("personalid"),""));
                chkPasswdInvalid.setSelected(rs.getInt("passwdinvalid")==1);
                txtPasswdExpires.setText(rs.getString("passwdexpires"));
                txtDatf.setText(rs.getString("datf"));
                txtDatt.setText(rs.getString("datt"));
                txtPasswd.setText("");
                String SQL2="select einrichtung from lusereinrichtung where username='"+username+"'";
                ResultSet rs2=st2.executeQuery(SQL2);
                DefaultListModel dlm=(DefaultListModel)lstEinrichtungen.getModel();
                lstEinrichtungen.clearSelection();                
                while (rs2.next()) {
                    for (int i=0;i<dlm.size();i++) {
                        ComboBoxEintrag cbe=(ComboBoxEintrag)dlm.get(i);
                        if (cbe.getKey().equals(rs2.getString("einrichtung"))) {
                            lstEinrichtungen.addSelectionInterval(i,i);
                        }
                    }                    
                }                
            } else {
                JOptionPane.showMessageDialog(this,"Schlüssel "+username+" nicht gefunden");
            }
            st.close();
            st2.close();
        } catch (SQLException e) {
            new SQLErrorDialog(this,"PG",e,SQL).setVisible(true);
        }
    }//GEN-LAST:event_tblUsersMouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        String username=txtUsername.getText();
        String personalid=((ComboBoxEintrag)cmbPersonalid.getSelectedItem()).getKey();
        String passwd = new String(txtPasswd.getPassword());
        String passwdclause="";
        if (passwd.length()>0) {
            passwd=new CryptedPasswd(passwd,username.substring(2)).getEncryptedPasswd();
            passwdclause=" passwd='"+passwd+"', ";
        }
        String passwdinvalid;
        if (chkPasswdInvalid.isSelected()) {
            passwdinvalid="1";
        } else {
            passwdinvalid="0";
        }
        String passwdexpires=txtPasswdExpires.getText();
        String datf=txtDatf.getText();
        String datt=txtDatt.getText();        
        String SQL="update xusers set personalid='"+personalid+"'," +
                passwdclause+
                "   passwdinvalid="+passwdinvalid+", "+
                "   passwdexpires=to_date('"+passwdexpires+"','DD.MM.YYYY')," +
                "   datf=to_date('"+datf+"','DD.MM.YYYY HH24:MI:SS')," +
                "   datt=to_date('"+datt+"','DD.MM.YYYY HH24:MI:SS') "+
                        " where username='"+username+"'";
        try {
            Statement st=db.createStatement();
            st.execute(SQL);
            SQL="delete from lusereinrichtung where username='"+username+"'";
            st.execute(SQL);
            for (int i=0;i<lstEinrichtungen.getSelectedValues().length;i++) {
                ComboBoxEintrag cbe=(ComboBoxEintrag)lstEinrichtungen.getModel().getElementAt(lstEinrichtungen.getSelectedIndices()[i]);
                SQL="insert into lusereinrichtung (username,einrichtung) values('"+username+"','"+cbe.getKey()+"')";
                st.execute(SQL);
            }                 
            st.close();
            refreshTable();
            if (passwd.length()>0) {
                new TimedDialog(this,true,"Passwort geändert",1000).setVisible(true);
            }
        } catch (SQLException e) {
            new SQLErrorDialog(this,"PG",e,SQL).setVisible(true);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        String username=txtUsername.getText();
        String SQL="";
        try {            
            Statement st=db.createStatement();
            SQL="delete from lusereinrichtung where username='"+username+"'";
            st.execute(SQL);            
            SQL="delete from xusers where username='"+username+"'";
            st.execute(SQL);
            st.close();
            refreshTable();
        } catch (SQLException e) {
            new SQLErrorDialog(this,"PG",e,SQL).setVisible(true);
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void btnAutoExpireActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAutoExpireActionPerformed
        SystemParameter s= new SystemParameter(db);
        NOW now=new NOW();
        now.setFormat("dd.MM.yyyy");
        int pwdexpiry=s.getIntParam("password","defaultPasswdExpiry",10);
        now.add(NOW.DATE, pwdexpiry);
        txtPasswdExpires.setText(now.getFormattedDate());
    }//GEN-LAST:event_btnAutoExpireActionPerformed
    

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAutoExpire;
    private javax.swing.JCheckBox chkPasswdInvalid;
    private javax.swing.JComboBox cmbPersonalid;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblBenutzername;
    private javax.swing.JLabel lblDatf;
    private javax.swing.JLabel lblDatt;
    private javax.swing.JLabel lblMitarbeiter;
    private javax.swing.JLabel lblPasswd;
    private javax.swing.JLabel lblPasswdExpires;
    private javax.swing.JLabel lblPasswdInvalid;
    private javax.swing.JList lstEinrichtungen;
    private javax.swing.JTable tblUsers;
    private javax.swing.JTextField txtDatf;
    private javax.swing.JTextField txtDatt;
    private javax.swing.JPasswordField txtPasswd;
    private javax.swing.JTextField txtPasswdExpires;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
    
}
